package com.google.code.timetrail.gui;


import java.awt.CardLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import com.google.code.timetrail.backend.Control;

public class StoreFrame extends JPanel implements DocumentListener{
    /**
     * 
     */
    private static final long serialVersionUID = -7890141237462582071L;
    private JLabel ammunitionLabel;
    private JLabel clothingLabel;
    private JPanel storePanel;
    private JLabel foodLabel;
    private JLabel enginesLabel;
    private JLabel priceAndWeightLabel;
    private JLabel itemNameLabel;
    private JLabel quantityLabel;
    private JLabel storeLabel;
    private JLabel fCapacitorLabel;
    private JLabel hullPartsLabel;
    private JLabel fuelCellsLabel;
    private JLabel enginePriceWeightLabel;
    private JLabel foodPriceWeightLabel;
    private JLabel clothingPriceWeightLabel;
    private JLabel ammunitionPriceWeightLabel;
    private JLabel fCapacitorsPriceWeightLabel;
    private JLabel hullPartsPriceWeightLabel;
    private JLabel fuelCellsPriceWeightLabel;
    private JTextField engineTextField;
    private JTextField foodTextField;
    private JTextField clothingTextField;
    private JTextField ammunitionTextField;
    private JTextField fCapacitorTextField;
    private JTextField hullPartsTextField;
    private JTextField fuelCellsTextField;
    private JLabel totalWeightLabel;
    private JLabel totalCostLabel;
    private JLabel moneyLabel;
    
    private JButton storeNextButton;
    private JButton storeBackButton;
    
    
    private StoreFrameBackend storeBackend;
    private Control myGameControl;
    private JPanel myCD;
    
    public StoreFrame(Control gameControl, JPanel cd){
        this.myGameControl = gameControl;
        this.myCD = cd;
        
        storeBackend = new StoreFrameBackend(gameControl);
        
        

        setLayout(null);
        
        storePanel = new JPanel();
        storePanel.setBounds(69, 34, 454, 373);
        add(storePanel);
        storePanel.setLayout(null);
        
        storeLabel = new JLabel(storeBackend.getStoreLabelText());
        storeLabel.setBounds(198, 6, 49, 16);
        storePanel.add(storeLabel);
        
        quantityLabel = new JLabel(storeBackend.getQuantityLabelText());
        quantityLabel.setBounds(341, 50, 54, 16);
        storePanel.add(quantityLabel);
        
        itemNameLabel = new JLabel(storeBackend.getItemNameLabelText());
        itemNameLabel.setBounds(39, 50, 28, 16);
        storePanel.add(itemNameLabel);
        
        priceAndWeightLabel = new JLabel(storeBackend.getPriceWeightLabelText());
        priceAndWeightLabel.setBounds(169, 50, 88, 16);
        storePanel.add(priceAndWeightLabel);
        
        enginesLabel = new JLabel(storeBackend.getEngineLabelText());
        enginesLabel.setBounds(39, 82, 61, 16);
        storePanel.add(enginesLabel);
        
        foodLabel = new JLabel(storeBackend.getFoodLabelText());
        foodLabel.setBounds(39, 110, 41, 16);
        storePanel.add(foodLabel);
        
        clothingLabel = new JLabel(storeBackend.getClothingLabelText());
        clothingLabel.setBounds(39, 138, 61, 16);
        storePanel.add(clothingLabel);
        
        ammunitionLabel = new JLabel(storeBackend.getAmmunitionLabelText());
        ammunitionLabel.setBounds(39, 166, 88, 16);
        storePanel.add(ammunitionLabel);
        
        fCapacitorLabel = new JLabel(storeBackend.getFCapacitorLabelText());
        fCapacitorLabel.setBounds(39, 194, 88, 16);
        storePanel.add(fCapacitorLabel);
        
        hullPartsLabel = new JLabel(storeBackend.getHullPartsLabelText());
        hullPartsLabel.setBounds(39, 222, 78, 16);
        storePanel.add(hullPartsLabel);
        
        fuelCellsLabel = new JLabel(storeBackend.getFuelCellsLabelText());
        fuelCellsLabel.setBounds(39, 249, 99, 16);
        storePanel.add(fuelCellsLabel);
        
        enginePriceWeightLabel = new JLabel(storeBackend.getEnginePriceWeight());
        enginePriceWeightLabel.setBounds(169, 82, 88, 16);
        storePanel.add(enginePriceWeightLabel);
        
        foodPriceWeightLabel = new JLabel(storeBackend.getFoodPriceWeight());
        foodPriceWeightLabel.setBounds(169, 110, 61, 16);
        storePanel.add(foodPriceWeightLabel);
        
        clothingPriceWeightLabel = new JLabel(storeBackend.getClothingPriceWeight());
        clothingPriceWeightLabel.setBounds(169, 138, 71, 16);
        storePanel.add(clothingPriceWeightLabel);
        
        ammunitionPriceWeightLabel = new JLabel(storeBackend.getAmmunitionPriceWeight());
        ammunitionPriceWeightLabel.setBounds(169, 166, 61, 16);
        storePanel.add(ammunitionPriceWeightLabel);
        
        fCapacitorsPriceWeightLabel = new JLabel(storeBackend.getFCapacitorsPriceWeight());
        fCapacitorsPriceWeightLabel.setBounds(169, 194, 81, 16);
        storePanel.add(fCapacitorsPriceWeightLabel);
        
        hullPartsPriceWeightLabel = new JLabel(storeBackend.getHullPartsPriceWeight());
        hullPartsPriceWeightLabel.setBounds(169, 222, 88, 16);
        storePanel.add(hullPartsPriceWeightLabel);
        
        fuelCellsPriceWeightLabel = new JLabel(storeBackend.getFuelCellsPriceWeight());
        fuelCellsPriceWeightLabel.setBounds(169, 249, 88, 16);
        storePanel.add(fuelCellsPriceWeightLabel);
        
        engineTextField = new JTextField();
        engineTextField.setHorizontalAlignment(SwingConstants.CENTER);
        engineTextField.setText("0");

        engineTextField.setBounds(341, 76, 54, 28);
        storePanel.add(engineTextField);
        engineTextField.setColumns(10);
        
        foodTextField = new JTextField();

        foodTextField.setHorizontalAlignment(SwingConstants.CENTER);
        foodTextField.setText("0");
        foodTextField.setBounds(341, 104, 54, 28);
        storePanel.add(foodTextField);
        foodTextField.setColumns(10);
        
        clothingTextField = new JTextField();

        clothingTextField.setText("0");
        clothingTextField.setHorizontalAlignment(SwingConstants.CENTER);
        clothingTextField.setBounds(341, 132, 54, 28);
        storePanel.add(clothingTextField);
        clothingTextField.setColumns(10);
        
        ammunitionTextField = new JTextField();

        ammunitionTextField.setText("0");
        ammunitionTextField.setHorizontalAlignment(SwingConstants.CENTER);
        ammunitionTextField.setBounds(341, 160, 54, 28);
        storePanel.add(ammunitionTextField);
        ammunitionTextField.setColumns(10);
        
        fCapacitorTextField = new JTextField();

        fCapacitorTextField.setText("0");
        fCapacitorTextField.setHorizontalAlignment(SwingConstants.CENTER);
        fCapacitorTextField.setBounds(341, 188, 54, 28);
        storePanel.add(fCapacitorTextField);
        fCapacitorTextField.setColumns(10);
        
        hullPartsTextField = new JTextField();

        hullPartsTextField.setText("0");
        hullPartsTextField.setHorizontalAlignment(SwingConstants.CENTER);
        hullPartsTextField.setBounds(341, 216, 54, 28);
        storePanel.add(hullPartsTextField);
        hullPartsTextField.setColumns(10);
        
        fuelCellsTextField = new JTextField();

        fuelCellsTextField.setHorizontalAlignment(SwingConstants.CENTER);
        fuelCellsTextField.setText("0");
        fuelCellsTextField.setBounds(341, 243, 54, 28);
        storePanel.add(fuelCellsTextField);
        fuelCellsTextField.setColumns(10);
        
        totalWeightLabel = new JLabel(storeBackend.getTotalWeightLabel());
        totalWeightLabel.setBounds(169, 290, 105, 16);
        storePanel.add(totalWeightLabel);
        
        totalCostLabel = new JLabel(storeBackend.getTotalCostLabel());
        totalCostLabel.setBounds(169, 318, 98, 16);
        storePanel.add(totalCostLabel);
        
        moneyLabel = new JLabel(storeBackend.getMoneyLabel());
        moneyLabel.setBounds(39, 290, 99, 16);
        storePanel.add(moneyLabel); 
        
        engineTextField.getDocument().addDocumentListener(this);
        foodTextField.getDocument().addDocumentListener(this);
        clothingTextField.getDocument().addDocumentListener(this);
        ammunitionTextField.getDocument().addDocumentListener(this);
        fCapacitorTextField.getDocument().addDocumentListener(this);
        hullPartsTextField.getDocument().addDocumentListener(this);
        fuelCellsTextField.getDocument().addDocumentListener(this);
        
        storeNextButton = new JButton(storeBackend.getNextButtonText());
        storeNextButton.addActionListener(new ActionListener() {
            

            public void actionPerformed(ActionEvent e) {
                
                
                
                

                
                storeBackend.pressNextButton();

            }
        });
        
        storeNextButton.setBounds(447, 418, 117, 29);
        add(storeNextButton);
        
        storeBackButton = new JButton(storeBackend.getBackButtonText());
        storeBackButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                storeBackend.pressBackButton();
                CardLayout cd = (CardLayout) myCD.getLayout();
                cd.previous(myCD);
            }
        });
        storeBackButton.setBounds(320, 418, 117, 29);
        add(storeBackButton);
    }

    @Override
    public void changedUpdate(DocumentEvent arg0) {
        // TODO Auto-generated method stub
        
    }

    @Override
    public void insertUpdate(DocumentEvent e) {

            storeBackend.setEngineTextField(engineTextField.getText());
            engineTextField.setBackground(storeBackend.getEngineTextFieldColor());
            
           

            storeBackend.setfoodTextField(foodTextField.getText());
            foodTextField.setBackground(storeBackend.getFoodTextFieldColor());
            
            

            storeBackend.setClothingTextField(clothingTextField.getText());
            clothingTextField.setBackground(storeBackend.getClothingTextFieldColor());

            storeBackend.setAmmunitionTextField(ammunitionTextField.getText());
            ammunitionTextField.setBackground(storeBackend.getAmmunitionTextFieldColor());

            storeBackend.setFCapacitorTextField(fCapacitorTextField.getText());
            fCapacitorTextField.setBackground(storeBackend.getFCapacitorTextFieldColor());

            storeBackend.setHullPartsTextField(hullPartsTextField.getText());
            hullPartsTextField.setBackground(storeBackend.getHullPartsTextFieldColor());

            storeBackend.setFuelCellsTextField(fuelCellsTextField.getText());
            fuelCellsTextField.setBackground(storeBackend.getFuelCellsTextFieldColor());

        
        engineTextField.setBackground(storeBackend.getEngineTextFieldColor());
        foodTextField.setBackground(storeBackend.getFoodTextFieldColor());
        clothingTextField.setBackground(storeBackend.getClothingTextFieldColor());
        ammunitionTextField.setBackground(storeBackend.getAmmunitionTextFieldColor());
        fCapacitorTextField.setBackground(storeBackend.getFCapacitorTextFieldColor());
        hullPartsTextField.setBackground(storeBackend.getHullPartsTextFieldColor());
        fuelCellsTextField.setBackground(storeBackend.getFuelCellsTextFieldColor());
        
        storeNextButton.setEnabled(storeBackend.canEnableNextButton());
        totalWeightLabel.setText(storeBackend.getTotalWeightLabel());
        totalCostLabel.setText(storeBackend.getTotalCostLabel());
    }
    @Override
    public void removeUpdate(DocumentEvent e) {


            storeBackend.setEngineTextField(engineTextField.getText());
            engineTextField.setBackground(storeBackend.getEngineTextFieldColor());
            
           

            storeBackend.setfoodTextField(foodTextField.getText());
            foodTextField.setBackground(storeBackend.getFoodTextFieldColor());
            

            storeBackend.setClothingTextField(clothingTextField.getText());
            clothingTextField.setBackground(storeBackend.getClothingTextFieldColor());

            storeBackend.setAmmunitionTextField(ammunitionTextField.getText());
            ammunitionTextField.setBackground(storeBackend.getAmmunitionTextFieldColor());

            storeBackend.setFCapacitorTextField(fCapacitorTextField.getText());
            fCapacitorTextField.setBackground(storeBackend.getFCapacitorTextFieldColor());

            storeBackend.setHullPartsTextField(hullPartsTextField.getText());
            hullPartsTextField.setBackground(storeBackend.getHullPartsTextFieldColor());

            storeBackend.setFuelCellsTextField(fuelCellsTextField.getText());
            fuelCellsTextField.setBackground(storeBackend.getFuelCellsTextFieldColor());

        
        engineTextField.setBackground(storeBackend.getEngineTextFieldColor());
        foodTextField.setBackground(storeBackend.getFoodTextFieldColor());
        clothingTextField.setBackground(storeBackend.getClothingTextFieldColor());
        ammunitionTextField.setBackground(storeBackend.getAmmunitionTextFieldColor());
        fCapacitorTextField.setBackground(storeBackend.getFCapacitorTextFieldColor());
        hullPartsTextField.setBackground(storeBackend.getHullPartsTextFieldColor());
        fuelCellsTextField.setBackground(storeBackend.getFuelCellsTextFieldColor());
        
        storeNextButton.setEnabled(storeBackend.canEnableNextButton());
        totalWeightLabel.setText(storeBackend.getTotalWeightLabel());
        totalCostLabel.setText(storeBackend.getTotalCostLabel());
    }
}
